function initialize(){
	tabela();
	novo();
}

function novo(){
	$('#conteudo').html(htmlLoading);
	$.post(baseUrlPost+"gastos/novo", function(data) {
		  $('#conteudo').html(data);
		  formatCampos();
	});
}

function formatCampos(){
	$("#data").datepicker();
	$("#data_de").datepicker();
	$("#data_ate").datepicker();
}

function editar(id){
	$('#conteudo').html(htmlLoading);
	$.post(baseUrlPost+"gastos/editar/"+id, function(data) {
		  $('#conteudo').html(data);
		  formatCampos();
	});
}

function excluir(id){
	if (confirm("Confirma excluir?")) { 
		$.post(baseUrlPost+"gastos/excluir/"+id, function(data) {
			  tabela();
		});
	}
}

function drawChartPie() {
	var hidden_categorias = $("#hidden_categorias").val().split("@@");
	var hidden_valores = $("#hidden_valores").val().split("@@");
	
	var ganhos = $("#hidden_ganhos").val();
	var gastos = $("#hidden_gastos").val();
	var gastos_ganho = $("#gastos_ganho").val();
	
	var rows = new Array();
	for(var i = 0; i<hidden_categorias.length;i++){
		if(hidden_valores[i]!=''){
			var row = new Array();
			row[0] = hidden_categorias[i]+" (R$ "+hidden_valores[i]+")";
			row[1] = parseFloat(hidden_valores[i]);
			rows[i]=row;
		}
	}
    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Categoria');
    data.addColumn('number', 'Valor');
    data.addRows(rows);

    // Set chart options
    var options = {'title':'Resumo dos gastos > R$'+ganhos+" - "+" R$"+gastos+" = "+" R$"+gastos_ganho,
                   'width':500,
                   'height':300,
                   'chartArea':{top:50,left:10,width:"100%",height:"100%"}
    };

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

function drawChartLine() {
	var data = new google.visualization.DataTable();
	var hidden_categorias = $("#hidden_categorias").val().split("@@");
    data.addColumn('string', 'Data');
    
    var hidden_datas = $("#hidden_datas").val().split("@@");
    
    
    
    var valores_categoria = new Array();
    for(var i = 0; i<hidden_categorias.length;i++){
		if(hidden_categorias[i]!=''){
			data.addColumn('number', hidden_categorias[i]);
			var hidden_valores_categoria = $("#hidden_valores_categoria"+i).val().split("@@");
			valores_categoria.push(hidden_valores_categoria);
		}
    }
    
    var rows = new Array();
    for(var i = 0; i<hidden_datas.length;i++){
    	if(hidden_datas[i]!=''){
    		var row = new Array();
    		row.push(hidden_datas[i]);		
    		for(var j = 0; j<valores_categoria.length;j++){
    			row.push(parseFloat(valores_categoria[j][i]));
    		}
    		rows.push(row);
    	}
    }
    
    data.addRows(rows);

    var options = {
      width: 600, height: 300
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div2'));
    chart.draw(data, options);
}

function tabela(){
	var form = $("#formulario_filtro").serialize();
	$('#tabela').html(htmlLoading);
	$.post(baseUrlPost+"gastos/tabela",form, function(data) {
		  $('#tabela').html(data);
		  formatCampos();
	});
}

function tabela_resumo(){
	var form = $("#formulario_filtro").serialize();
	$('#content_resize').html(htmlLoading);
	$.post(baseUrlPost+"gastos/tabelaresumo",form, function(data) {
		  $('#content_resize').html(data);
		  formatCampos();
		  drawChartPie();
		  drawChartLine();
	});
}

function gravar(){
	var form = $("#formulario").serialize();
	$('#conteudo').html(htmlLoading);
	$.post(baseUrlPost+"gastos/gravar",form, function(data) {
		  $('#conteudo').html(data);
		  tabela();
		  formatCampos();
	});
}